package com.tywj.buscustomerapp.view.item;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.DateJoe;
import com.tywj.buscustomerapp.utils.TimeRender;
import com.tywj.buscustomerapp.utils.myadapter.item.AdapterItem;


public class DateItem implements AdapterItem<DateJoe> {

    private LinearLayout dateLine;
    private TextView day;
    private TextView price;
    private TextView count;
    private OnCheckListener listener;


    public void setListener(OnCheckListener listener) {
        this.listener = listener;
    }

    public interface OnCheckListener{
        void onChecked(DateJoe dateJoe,int position,View view);
        void onDeleted(DateJoe dateJoe);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.item_date;
    }

    @Override
    public void bindViews(@NonNull View root) {
        dateLine = root.findViewById(R.id.dateLin);
        day = root.findViewById(R.id.day);
        price = root.findViewById(R.id.price);
        root.setBackgroundColor(root.getResources().getColor(R.color.white));
        day.setTextColor(root.getResources().getColor(R.color.text));
        price.setTextColor(root.getResources().getColor(R.color.text_price));
//        price.setBackgroundColor(root.getResources().getColor(R.color.appColor));
        count = root.findViewById(R.id.count);
    }

    @Override
    public void setViews() {

    }

    private void setChecked(View v,final DateJoe dateJoe){

    }

    public void setUnChecked(View v,final DateJoe dateJoe){

    }

    @Override
    public void handleData(final DateJoe dateJoe, final int position) {
        Log.i("fuck",dateJoe.getWeek()+"==="+dateJoe.getDay()+"===="+dateJoe.isShow);
        price.setText(dateJoe.getPrice());
        day.setText(dateJoe.getDay());
        if (!dateJoe.isShow){
            day.setTextColor(Color.GRAY);
            count.setTextColor(dateLine.getResources().getColor(R.color.textgray));
            if (dateJoe.getWeek()==0 || dateJoe.getWeek()==6) {
                price.setBackgroundColor(dateLine.getResources().getColor(R.color.bggray));
                price.setTextColor(dateLine.getResources().getColor(R.color.textgray));
                count.setVisibility(View.GONE);
                price.setVisibility(View.GONE);
            }else{
                price.setVisibility(View.GONE);
                count.setText("售罄");
                count.setVisibility(View.VISIBLE);
            }
        }else{
            if (dateJoe.isCheck){
                dateLine.setBackgroundDrawable(dateLine.getResources().getDrawable(R.drawable.bg_ori_shape1));
                day.setTextColor(dateLine.getResources().getColor(R.color.text));
                price.setTextColor(dateLine.getResources().getColor(R.color.text_price));
//            price.setBackgroundColor(dateLine.getResources().getColor(R.color.white));
                count.setText("余："+dateJoe.getCount());
                count.setVisibility(View.VISIBLE);
            }else{
                dateLine.setBackgroundColor(dateLine.getResources().getColor(R.color.white));
                day.setTextColor(dateLine.getResources().getColor(R.color.text));
                price.setTextColor(dateLine.getResources().getColor(R.color.text_price));
//            price.setBackgroundColor(dateLine.getResources().getColor(R.color.appColor));
                count.setVisibility(View.GONE);
            }
        }

        if (dateJoe.isBuyed()){
            count.setText("已买");
        }

        dateLine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dateJoe.isShow) {
                    if (!dateJoe.isCheck) {
                        if (TimeRender.compareTime3(dateJoe.getTotalDate())
//                                position != 0 && position % 7 != 0 && position != 6 && position != 13 && position != 20
                                ) {
                            v.setBackgroundDrawable(dateLine.getResources().getDrawable(R.drawable.bg_ori_shape1));
//                            v.setBackgroundColor(v.getResources().getColor(R.color.appColor));
                            day.setTextColor(v.getResources().getColor(R.color.white));
                            price.setTextColor(v.getResources().getColor(R.color.text_price));
                            dateJoe.isCheck = true;
                            listener.onChecked(dateJoe, position, v);
                        }
                    } else {
                        if (TimeRender.compareTime3(dateJoe.getTotalDate())
//                                position != 0 && position % 7 != 0 && position != 6 && position != 13 && position != 20
                                ) {
                            v.setBackgroundColor(v.getResources().getColor(R.color.white));
                            day.setTextColor(v.getResources().getColor(R.color.black));
                            price.setTextColor(v.getResources().getColor(R.color.text_price));
                            dateJoe.isCheck = false;
                            listener.onDeleted(dateJoe);
                            count.setVisibility(View.GONE);
                        }
                    }
                }
            }
        });
    }
}
